package com.sgb.kjwl.view.widget.manage;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.view.ui.adapter.pick.PickAreaAdapter;
import com.sgb.kjwl.view.widget.MaxHeightRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class IPRAreaPopWindow extends BasePopWindow {

    private MaxHeightRecyclerView recyclerView;
    private MaxHeightRecyclerView cityList;
    private LinearLayout ll_next;
    private TextView tv_next;
    private TextView tv_ok;
    private TextView tv_s;
    private LinearLayout bottomLayout;
    private PickAreaAdapter pickProvinceAdapter;
    private PickAreaAdapter pickCityAdapter;
    private List<PickSelectEntity> cityDatas;
    private String oldNo = "";
    private String no = "";
    private String oneNo = "";
    private String towNo = "";
    private String name = "";
    private String name2 = "";
    private int position = -1;
    private boolean isNext;
    private IButtonClick iButtonClick;

    public interface IButtonClick {
        void next(String no);
    }

    public void setiButtonClick(IButtonClick iButtonClick) {
        this.iButtonClick = iButtonClick;
    }

    public IPRAreaPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.ipr_popwindow_e_area;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        cityList = holder.getPop().findViewById(R.id.city_list);
        bottomLayout = holder.getPop().findViewById(R.id.bottom_layout);
        ll_next = holder.getPop().findViewById(R.id.ll_next);
        tv_next = holder.getPop().findViewById(R.id.tv_next);
        tv_ok = holder.getPop().findViewById(R.id.tv_ok);
        tv_s = holder.getPop().findViewById(R.id.tv_s);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isNext) {
                    if (!oneNo.equals("")) {
                        if (onResultClick != null) {
                            if (oneNo == null)
                                oneNo = "";
                            onResultClick.result(oneNo, name, true);
                            Log.d("省份编号", oneNo);
                        }
                    }
                } else {
                  /*  if (towNo.equals("")) {
                        if (onResultClick != null) {
                            if (towNo == null)
                                towNo = "";
                            onResultClick.result(towNo, name, position);
                            Log.d("城市编号", towNo);
                        } else {
                            if (onResultClick != null) {
                                if (oneNo == null)
                                    oneNo = "";
                                onResultClick.result(oneNo, name, position);
                                Log.d("省份编号", oneNo);
                            }
                        }
                    }*/
                    if (onResultClick != null) {
                        if (oneNo == null)
                            oneNo = "";
                        onResultClick.result(oneNo, name, true);
                        Log.d("省份编号", oneNo);
                    }
                }
                dissMiss();
            }
        });

        tv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isNext) {
                    if (!oldNo.equals(oneNo)) {
                        oldNo = oneNo;
                        iButtonClick.next(oneNo);
                    } else {
                        if (!oneNo.equals("")) {
                            tv_next.setVisibility(View.GONE);
                            setGone(View.GONE, View.VISIBLE, "上一步", true);
                        } else {
                            Toast.makeText(mContext, "请选择一个省份", Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    //隐藏布局按钮
                    isVisibleBottomLayout(false);
                    setGone(View.VISIBLE, View.GONE, "下一步", false);
                }
            }
        });
        pickProvinceAdapter = new PickAreaAdapter(mContext, null, R.layout.item_pick_area_choose);
        pickProvinceAdapter.setItemSelect(true);
        pickProvinceAdapter.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(pickProvinceAdapter);
        pickProvinceAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                oneNo = pickSelectEntity.getNo();
                name = pickSelectEntity.getProjectscale();
                if (name.equals("不限")) {
                    dissMiss();
                    if (onResultClick != null) {
                        if (oneNo == null)
                            oneNo = "";
                        onResultClick.result(oneNo, name, true);
                    }
                    Log.d("省份编号", oneNo);
                } else {
                    //设置列表显示
                    setGone(View.GONE, View.VISIBLE, "上一步", true);
                    //查询城市数据
                    iButtonClick.next(oneNo);
                    tv_s.setText(name);
                    //显示按钮布局
                    isVisibleBottomLayout(true);
                    onResultClick.result(oneNo, name, true);
                }
            }
        });
        pickCityAdapter = new PickAreaAdapter(mContext, null, R.layout.item_pick_area_choose);
        pickCityAdapter.setItemSelect(true);
        pickCityAdapter.setDefItem(0);
        cityList.setLayoutManager(new GridLayoutManager(mContext, 4));
        cityList.setAdapter(pickCityAdapter);
        pickCityAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                towNo = pickSelectEntity.getNo();
                name2 = pickSelectEntity.getProjectscale();
                if (onResultClick != null) {
                    if (towNo == null)
                        towNo = "";
                    onResultClick.result(towNo, name2, false);
                    Log.d("城市编号", towNo);
                    dissMiss();
                }
            }
        });
    }

    public void setData(List<PickSelectEntity> cityDatas) {
        this.cityDatas = cityDatas;
        pickProvinceAdapter.setDatas(cityDatas);
        tv_s.setText(cityDatas.get(0).getProjectscale());
    }

    public void setData(List<PickSelectEntity> cityDatas, int position) {
        this.cityDatas = cityDatas;
        pickProvinceAdapter.setDatas(cityDatas);
        pickProvinceAdapter.setDefItem(position);
    }

    public void isVisibleBottomLayout(boolean visible) {
        ll_next.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setCityDatas(List<PickSelectEntity> cityDatas) {
        if (cityDatas != null && cityDatas.size() != 0) {
            pickCityAdapter.setDefItem(0);
            setGone(View.GONE, View.VISIBLE, "上一步", true);
            pickCityAdapter.setDatas(cityDatas);
            isNext = true;

        }
    }

    private void setGone(int isV, int isV2, String text, boolean is) {
        recyclerView.setVisibility(isV);
        cityList.setVisibility(isV2);
        tv_next.setText(text);
        isNext = is;
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }
}
